const path = require('path');
const fs = require('fs');
const url = require('url');
const express = require('express');

// 创建服务器
const app = express();

app.listen(3000, () => {
    console.log('服务器已开启');
})

app.get('/login', (req, res) => {
    res.sendFile(path.join(__dirname, '/login.html'));
})

app.get('/register', (req, res) => {
    res.sendFile(path.join(__dirname, '/register.html'));
})

let a = JSON.parse(fs.readFileSync(path.join(__dirname, 'persons.json')));
app.get('/dologin', (req, res) => {

    let user = req.query;
    let tag = a.some(item => item.username == user.username && item.password == user.password);
    if (tag) {
        res.send('登入成功');
    }
    else {
        res.send('账号密码错误');
    }
})

app.get('/doregister', (req, res) => {
    let user = req.query;
    let tag = a.some(item => item.username == user.username);
    if (tag) {
        res.send('该账号已注册')
    }
    else {
        a.push(user);
        fs.writeFileSync(path.join(__dirname, 'persons.json'), JSON.stringify(a));
        res.send('注册成功')
    }
})

app.get('/', (req, res) => {
    res.send('404');
})
